﻿using CatUtils.Utils.Currency;
using CatUtils.Utils.Currency.IoUtils;
using CatUtils.Utils.Currency.TextUtils;
using CatUtils.Utils.SpecialUtils.View;
using RabbitDownload.Helper.Currency;

using System;
using System.Collections.Generic;
using System.IO;

namespace RabbitDownload.WebSite.WebCurrency
{
    internal class WebSiteHelper
    {   /// <summary>
        /// 刷新日志
        /// </summary>
        /// <param name="LogList"></param>
        /// <param name="Text"></param>
        /// <param name="FilePath"></param>
        public static void RefreshLog(ref List<string> LogList, string Text, String FilePath)
        {
            if (LogList.Contains(Text))
            {
                Console.WriteLine("该日志存在，不重复添加");
                return;
            }
            LogList.Add(Text);
            FileHelper.AppendText(FilePath, Text);
        }

        /// <summary>
        /// 选择并且切割文件
        /// </summary>
        public static void SelcetSplit()
        {
            String FilePath = FileHelper.SelectFile(AppDomain.CurrentDomain.BaseDirectory + Helper.StaticValueOld.SaveListPath.Substring(2));
            if (FilePath != "")
            {
                ListSplit(FilePath);
                if (MessageHelper.DialogResultAlert("打开存放路径？"))
                    FloderHelper.OpenFloder(FloderNameHelper.GetFloderPath(FilePath));
            }
        }

        /// <summary>
        /// 切割并且保存字符串
        /// </summary>
        /// <param name="FilePath"></param>
        public static string ListSplit(string FilePath)
        {
            String FileName = FileNameHelper.GetFileName(FilePath);
            if (FilePath == "" || FileName == "")
                return "";

            string Floder = FloderNameHelper.GetFloderPath(FilePath) + string.Format("已切割{0}{1}\\",
                Path.GetFileNameWithoutExtension(FileName)
                , TextHelper.ReplaceBadChar(DateTime.Now.ToLongTimeString().ToString()));
            FloderHelper.FloderExits(Floder, true);

            int count = 1;
            int FileCount = 1;
            foreach (string line in TextHelper.ReadAllLine(FilePath))
            {
                count++;
                FileHelper.AppendText(Floder + string.Format("已切割（{0}）", FileCount) + FileName, line);

                if (count % 998 == 0)
                {
                    count = 1;
                    FileCount++;
                }
            }
            return Floder;
        }
    }
}